package com.dullwolf.utils;

import com.dullwolf.pojo.CqResponse;

import java.util.HashMap;
import java.util.Map;

//将CQ的HTTP API封装为接口，并托管到Spring
public class CqUtil {

    private static String baseURL = "http://localhost:" + PropertiesUtil.readKey("port");

    public static CqResponse sendMsg(long id, String msg, String type) {
        Map<String, Object> info = new HashMap<>();
        String URL;
        switch (type) {
            case "group":
                URL = baseURL + "/send_group_msg";
                info.put("group_id", id);
                info.put("message", msg);
                break;
            case "discuss":
                URL = baseURL + "/send_discuss_msg";
                info.put("discuss_id", id);
                info.put("message", msg);
                break;
            case "private":
                URL = baseURL + "/send_private_msg";
                info.put("user_id", id);
                info.put("message", msg);
                break;
            default:
                return null;
        }
        String result = HttpRequest.doPost(JsonUtils.getJsonString(info), URL);
        return JsonUtils.getObjectByJson(result, CqResponse.class);
    }

    public static CqResponse getMemberInfo(long gId) {
        Map<String, Object> info = new HashMap<>();
        String URL = baseURL + "/get_group_member_list";
        info.put("group_id", gId);
        String result = HttpRequest.doPost(JsonUtils.getJsonString(info), URL);
        return JsonUtils.getObjectByJson(result, CqResponse.class);
    }

    public static CqResponse agreeFriend(String flag) {
        Map<String, Object> info = new HashMap<>();
        String URL = baseURL + "/set_friend_add_request";
        info.put("flag", flag);
        info.put("approve", true);
        String result = HttpRequest.doPost(JsonUtils.getJsonString(info), URL);
        return JsonUtils.getObjectByJson(result, CqResponse.class);
    }

    public static CqResponse agreeGroup(String flag, String subType) {
        Map<String, Object> info = new HashMap<>();
        String URL = baseURL + "/set_group_add_request";
        info.put("flag", flag);
        info.put("type", subType);
        info.put("approve", true);
        String result = HttpRequest.doPost(JsonUtils.getJsonString(info), URL);
        return JsonUtils.getObjectByJson(result, CqResponse.class);
    }

    public static CqResponse banGroupMember(long gid, long uid) {
        Map<String, Object> info = new HashMap<>();
        String URL = baseURL + "/set_group_ban";
        info.put("group_id", gid);
        info.put("user_id", uid);
        info.put("duration", 10 * 60);//默认禁言10分钟，不要太狠了
        String result = HttpRequest.doPost(JsonUtils.getJsonString(info), URL);
        return JsonUtils.getObjectByJson(result, CqResponse.class);
    }

    public static CqResponse deleteMsg(Integer msgId) {
        Map<String, Object> info = new HashMap<>();
        String URL = baseURL + "/delete_msg";
        info.put("message_id", msgId);
        String result = HttpRequest.doPost(JsonUtils.getJsonString(info), URL);
        return JsonUtils.getObjectByJson(result, CqResponse.class);
    }

    public static CqResponse getFriendList() {
        Map<String, Object> info = new HashMap<>();
        String URL = baseURL + "/_get_friend_list ";
        info.put("flat", false);
        String result = HttpRequest.doPost(JsonUtils.getJsonString(info), URL);
        return JsonUtils.getObjectByJson(result, CqResponse.class);
    }
}
